<?php

namespace wap\models;

use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%categories}}".
 *
 * @property string $id 菜单 ID
 * @property string $series_id 系列 ID
 * @property string $parent_id 父级
 * @property string $name 名称
 * @property int $child 有子级
 * @property string $parent_arr 父级链
 * @property string $child_arr 子级群
 * @property string $sort 排序
 * @property int $status 状态
 *
 * @property \wap\models\Series $series
 * @property \wap\models\Categories $parent
 * @property \wap\models\Categories[] $childs
 */
class Categories extends \common\models\Categories
{

    public function afterFind()
    {
        parent::afterFind();

        $this->parent_arr = explode(",", $this->parent_arr);
        $this->child_arr = explode(",", $this->child_arr);
    }

    public function getParent()
    {
        return $this->hasOne(Categories::className(), ['id' => 'parent_id']);
    }

    public function getChilds()
    {
        return $this->hasMany(Categories::className(), ['parent_id' => 'id']);
    }

    public static function getNavs()
    {
        $return[] = [
            'url' => Url::to(['case/index']),
            'name' => '门型',
            'active' => true,
            'childs' => self::getCateNavs(),
        ];

        return $return;
    }

    private static function getCateNavs()
    {
        $categoryIdInput = Yii::$app->request->getQueryParam('category_id');
        $categories = self::find()->all();

        return array_map(function($category)use($categoryIdInput){
            return [
                'url' => Url::to(['product/index', 'category_id' => $category->id]),
                'name' => $category->name,
                'active' => $category->id == $categoryIdInput ? 'active' : '',
            ];
        }, $categories);
    }
}
